package com.jwt.Handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jwt.pojo.ResponseResult;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
	@Resource
	ObjectMapper objectMapper;
	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
		response.setContentType("application/json;charset=utf-8");
		ResponseResult<?> result=new ResponseResult<>();
		result.setCode(2);
		result.setFlag(false);
		result.setMsg("尚未登录，或者登录过期");
		String s = objectMapper.writeValueAsString(result);
		response.getWriter().write(s);
	}
}
